package cn.Robbery.org.service.impl;

import cn.Robbery.basic.exception.BusinessException;
import cn.Robbery.basic.service.impl.BaseServiceImpl;
import cn.Robbery.org.domain.Department;
import cn.Robbery.org.domain.Employee;
import cn.Robbery.org.domain.Shop;
import cn.Robbery.org.mapper.DepartmentMapper;
import cn.Robbery.org.mapper.EmployeeMapper;
import cn.Robbery.org.mapper.ShopMapper;
import cn.Robbery.org.service.IDepartmentService;
import cn.Robbery.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop>
        implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void settledIn(Shop shop) {
        Employee admin = shop.getAdmin();

        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if(employee != null){
            throw new BusinessException("你已经入驻过，请直接登录");
        }
        employeeMapper.save(admin);
        shop.setAdmin(admin);
        shopMapper.save(shop);
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
